/**
 * Created by zengy on 2017/6/12.
 */

angular.module('mainApp.doctorManage',['ui.router'])
    .config(function ($stateProvider) {
        $stateProvider
            .state({
                name:'doctorManage/doctorList',
                url:'/doctorManage/doctorList',
                templateUrl:'views/doctorManage/doctor_list.html?v='+new Date().getMinutes(),
                controller:'doctorManageCtrl'
            })
            .state({
                name:'doctorManage/doctorSetup',
                url:'/doctorManage/doctorSetup/:doctorId/:doctorCallIsOpen/:expertConsultationIsOpen/:expertConsultationRowId/:doctorCallRowId',
                templateUrl:'views/doctorManage/doctor_setup.html?v='+new Date().getMinutes(),
                controller:'doctorSetupCtrl'
            })
    })
    .controller('doctorManageCtrl',function ($scope) {
        $scope.$emit('arg',4)

        $scope.doctorModal = {
            pageNo: 1,
            pageSize: 10
        }


        //加载分页插件
        $scope.pagination = function (currentPage, pageCount) {
            $('.page-function #pagination').remove();
            $('.page-function').html('<ul id="pagination" class="pagination"></ul>');
            $('.pagination').twbsPagination({
                totalPages: pageCount,
                startPage: currentPage,
                visiblePages: 5,
                first: "首页",
                prev: "上一页",
                next: "下一页",
                last: "尾页",
                href: 'javascript:;',
                //hideOnlyOnePage : true,
                onPageClick: function (event, page) {
                    $scope.getDocList(page)
                }
            });
        };
        //医生列表
        $scope.getDocList = function (page) {
            if (page) {
                $scope.doctorModal.pageNo = page;
            }

            layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
            new HttpRequest().getDoctorList($scope.doctorModal, function (res) {
                layer.closeAll();
                if (res.code == '000') {
                    $scope.docList = res.data.list;
                    $scope.$apply();
                    if ($scope.docList.length > 0) {
                        $scope.pagination($scope.doctorModal.pageNo, res.data.pageCount);
                    }
                } else {
                    layer.msg(res.msg)
                }
            })
        }
        $scope.getDocList($scope.doctorModal.pageNo);
        //设置医生服务项 动态绑定参数
        $scope.doctorCallRowId="";
        $scope.expertConsultationRowId="";
        $scope.setServer = function (event) {

            var _this = $(event.target).closest('td');
            var callOpen=_this.attr('data-callOpen');
            var expertOpen=_this.attr('data-expertOpen');
            $('select[name="expertConsultationRowId"]').find("option[value='"+expertOpen+"']").attr("selected",'selected');
            $('select[name="doctorCallRowId"]').find("option[value='"+callOpen+"']").attr("selected",'selected');
            $scope.doctorCallRowId = _this.data('call');
            $scope.expertConsultationRowId = _this.data('expert');
        }
        $scope.commitTel=function () {
           var  rowIds=[$scope.expertConsultationRowId,$scope.doctorCallRowId];
           var  isOpens=[$('select[name="expertConsultationRowId"]').val(),$('select[name="doctorCallRowId"]').val()];
            new HttpRequest().changeDoctorServiceV2({rowIds:rowIds,isOpens:isOpens},function (res) {
                if(res.code=='000'){
                    location.reload()
                }else{
                    layer.msg(res.msg)
                }
            })
        }

        //开通专家一对一见面
        /*
        $('select[name="expertConsultationRowId"]').change(function () {
            if(!$(this).val()){
                return false;
            }
            new HttpRequest().changeDoctorService({rowId:$scope.expertConsultationRowId,isOpen:$(this).val()},function (res) {
                if(res.code=='000'){
                    location.reload()
                }else{
                    layer.msg(res.msg)
                }
            })
        })
        //电话咨询
        $('select[name="doctorCallRowId"]').change(function () {
            if(!$(this).val()){
                return false;
            }
            new HttpRequest().changeDoctorService({rowId:$scope.doctorCallRowId,isOpen:$(this).val()},function (res) {
                if(res.code=='000'){
                    location.reload()
                }else{
                    layer.msg(res.msg)
                }
            })
        })*/

    })
    .controller('doctorSetupCtrl',function ($scope,$stateParams) {
        $scope.$emit('arg',4)
        //医生排班信息

        $scope.doctorId=$stateParams.doctorId;
        $scope.doctorCallIsOpen=$stateParams.doctorCallIsOpen;
        $scope.expertConsultationIsOpen=$stateParams.expertConsultationIsOpen;
        $scope.expertConsultationRowId=$stateParams.expertConsultationRowId;//一对一服务ID
        $scope.doctorCallRowId=$stateParams.doctorCallRowId;//电话服务ID
        $scope.show=1;
        $scope.editorDocModal={};
        $scope.editorCallModal={};

        //判断排班时间是否显示
        $scope.showTime=true;
        $scope.showScheduling=true
        if($scope.doctorCallIsOpen=='0'){
            $scope.showTime=false;

        }
        if($scope.expertConsultationIsOpen=='0'){
            $scope.showScheduling=false;

        }

        //显示排班信息
        new HttpRequest().getDoctorSchedualMsg({doctorId:$scope.doctorId},function (res) {
            if(res.code=='000'){
                var SchedulingList=res.data || [];
                SchedulingList.map(function (index) {
                    var startIndex= index.startTime.substring(0,index.startTime.indexOf(":"));
                    var endIndex=index.endTime.substring(0,index.endTime.indexOf(":"));
                    var startSec=index.startTime.substring(index.startTime.indexOf(":")+1,index.startTime.length);
                    var endSec=index.endTime.substring(index.endTime.indexOf(":")+1,index.endTime.length);
                    var num=(parseInt(endIndex)-parseInt(startIndex)) *3 + ( Math.floor((parseInt(endSec)-parseInt(startSec))/20) < 0 ? -1 :Math.floor((parseInt(endSec)-parseInt(startSec))/20));
                    index.reservationNumber= (num<0 ? 0 :num )
                });
                //console.log(SchedulingList);
                for(var i=0;i<SchedulingList.length;i++){
                    if(SchedulingList[i].periodTime==0){
                        var morningEle=$('.morning');
                        switch (SchedulingList[i].week){
                            case '1':
                                //console.log(SchedulingList[i])
                                morningEle.find('td:eq(1)').html(SchedulingList[i].timeInterval+'<br />&yen;'+SchedulingList[i].money).data('dataDoc',SchedulingList[i]);
                                break;
                            case '2':
                                morningEle.find('td:eq(2)').html(SchedulingList[i].timeInterval+'<br />&yen;'+SchedulingList[i].money).data('dataDoc',SchedulingList[i]);
                                break;
                            case '3':
                                morningEle.find('td:eq(3)').html(SchedulingList[i].timeInterval+'<br />&yen;'+SchedulingList[i].money).data('dataDoc',SchedulingList[i]);
                                break;
                            case '4':
                                morningEle.find('td:eq(4)').html(SchedulingList[i].timeInterval+'<br />&yen;'+SchedulingList[i].money).data('dataDoc',SchedulingList[i]);
                                break;
                            case '5':
                                morningEle.find('td:eq(5)').html(SchedulingList[i].timeInterval+'<br />&yen;'+SchedulingList[i].money).data('dataDoc',SchedulingList[i]);
                                break;
                            case '6':
                                morningEle.find('td:eq(6)').html(SchedulingList[i].timeInterval+'<br />&yen;'+SchedulingList[i].money).data('dataDoc',SchedulingList[i]);
                                break;
                            case '7':
                                morningEle.find('td:eq(7)').html(SchedulingList[i].timeInterval+'<br />&yen;'+SchedulingList[i].money).data('dataDoc',SchedulingList[i]);
                                break;

                        }
                    }else if(SchedulingList[i].periodTime==1){
                        var afternoonEle=$('.afternoon');
                        switch (SchedulingList[i].week){
                            case '1':
                                afternoonEle.find('td:eq(1)').html(SchedulingList[i].timeInterval+'<br />&yen;'+SchedulingList[i].money).data('dataDoc',SchedulingList[i]);
                                break;
                            case '2':
                                afternoonEle.find('td:eq(2)').html(SchedulingList[i].timeInterval+'<br />&yen;'+SchedulingList[i].money).data('dataDoc',SchedulingList[i]);
                                break;
                            case '3':
                                afternoonEle.find('td:eq(3)').html(SchedulingList[i].timeInterval+'<br />&yen;'+SchedulingList[i].money).data('dataDoc',SchedulingList[i]);
                                break;
                            case '4':
                                afternoonEle.find('td:eq(4)').html(SchedulingList[i].timeInterval+'<br />&yen;'+SchedulingList[i].money).data('dataDoc',SchedulingList[i]);
                                break;
                            case '5':
                                afternoonEle.find('td:eq(5)').html(SchedulingList[i].timeInterval+'<br />&yen;'+SchedulingList[i].money).data('dataDoc',SchedulingList[i]);
                                break;
                            case '6':
                                afternoonEle.find('td:eq(6)').html(SchedulingList[i].timeInterval+'<br />&yen;'+SchedulingList[i].money).data('dataDoc',SchedulingList[i]);
                                break;
                            case '7':
                                afternoonEle.find('td:eq(7)').html(SchedulingList[i].timeInterval+'<br />&yen;'+SchedulingList[i].money).data('dataDoc',SchedulingList[i]);
                                break;

                        }
                    }else if(SchedulingList[i].periodTime==2){
                        var nightEle=$('.night');
                        switch (SchedulingList[i].week){
                            case '1':
                                nightEle.find('td:eq(1)').html(SchedulingList[i].timeInterval+'<br />&yen;'+SchedulingList[i].money).data('dataDoc',SchedulingList[i]);
                                break;
                            case '2':
                                nightEle.find('td:eq(2)').html(SchedulingList[i].timeInterval+'<br />&yen;'+SchedulingList[i].money).data('dataDoc',SchedulingList[i]);
                                break;
                            case '3':
                                nightEle.find('td:eq(3)').html(SchedulingList[i].timeInterval+'<br />&yen;'+SchedulingList[i].money).data('dataDoc',SchedulingList[i]);
                                break;
                            case '4':
                                nightEle.find('td:eq(4)').html(SchedulingList[i].timeInterval+'<br />&yen;'+SchedulingList[i].money).data('dataDoc',SchedulingList[i]);
                                break;
                            case '5':
                                nightEle.find('td:eq(5)').html(SchedulingList[i].timeInterval+'<br />&yen;'+SchedulingList[i].money).data('dataDoc',SchedulingList[i]);
                                break;
                            case '6':
                                nightEle.find('td:eq(6)').html(SchedulingList[i].timeInterval+'<br />&yen;'+SchedulingList[i].money).data('dataDoc',SchedulingList[i]);
                                break;
                            case '7':
                                nightEle.find('td:eq(7)').html(SchedulingList[i].timeInterval+'<br />&yen;'+SchedulingList[i].money).data('dataDoc',SchedulingList[i]);
                                break;
                        }
                    }
                }

            }else{
                layer.msg(res.code)
            }
        })
        //查询所有可设置执业地点信息
        new HttpRequest().queryEnableAddress(function (res) {
            if(res.code=='000'){
                $scope.addressList=res.data
            }
        })

        //修改添加医生排班
        $scope.chose=function (event) {
            var _this=$(event.target);
            var index=_this.index();
            var dataDoc=_this.data('dataDoc');
            var parent=_this.closest('tr')[0];
            if(parent.className=='morning'){
                $scope.show=1;
            }else if(parent.className=='afternoon'){
                $scope.show=2;
            }else{
                $scope.show=3;
            }
            //console.log($scope.show);
            if(!dataDoc){
                $scope.editorDocModal.week=index;
                $scope.editorDocModal.doctorId=$scope.doctorId;
                $scope.editorDocModal.rowId="";
                $scope.editorDocModal.wxServiceId=$scope.expertConsultationRowId;
                if(parent.className=='morning'){
                    $scope.editorDocModal.periodTime='0';
                }else if(parent.className=='afternoon'){
                    $scope.editorDocModal.periodTime='1';
                }else{
                    $scope.editorDocModal.periodTime='2';
                }
            }else{
                console.log(dataDoc);
                //$scope.editorDocModal=null;
                var startIndex=dataDoc.startTime.split(':')[0];
                var endIndex=dataDoc.startTime.split(':')[1];
                var startSec=dataDoc.endTime.split(":")[0];
                var endSec=dataDoc.endTime.split(":")[1];
                setTimeout(function () {
                    $('select[name="startIndex"]').find("option[value='"+startIndex+"']").prop("selected",true);
                    $('select[name="startSec"]').find("option[value='"+endIndex+"']").prop("selected",true);
                    $('select[name="endIndex"]').find("option[value='"+startSec+"']").prop("selected",true);
                    $('select[name="endSec"]').find("option[value='"+endSec+"']").prop("selected",true);
                    $('select[name="address"]').find("option[value='"+dataDoc.addressId+"']").prop("selected",true);
                },100)

                dataDoc.wxServiceId=$scope.expertConsultationRowId;
                $scope.editorDocModal=dataDoc;
            }



        }
        $('body').on('change','.show-time select',function () {
            var startIndex=$('select[name="startIndex"]').val();
            var startSec=$('select[name="startSec"]').val();
            var endIndex=$('select[name="endIndex"]').val();
            var endSec=$('select[name="endSec"]').val();
            var num= (parseInt(endIndex)-parseInt(startIndex)) *3 + ( Math.floor((parseInt(endSec)-parseInt(startSec))/20) < 0 ? -1 :Math.floor((parseInt(endSec)-parseInt(startSec))/20));
            $('input[name="reservationNumber"]').val(num);
        })

        //提交
        $scope.commit=function () {
            var startIndex=$('select[name="startIndex"]').val();
            var startSec=$('select[name="startSec"]').val();
            var endIndex=$('select[name="endIndex"]').val();
            var endSec=$('select[name="endSec"]').val();
            $scope.editorDocModal.startTime=startIndex+':'+startSec;
            $scope.editorDocModal.endTime=endIndex+":"+endSec;
            if($('input[name="reservationNumber"]').val() <=0){
                layer.msg('开始时间大于结束时间！');
                return false;
            }
            $scope.editorDocModal.reservationNumber=$('input[name="reservationNumber"]').val();
            $scope.editorDocModal.addressId=$('select[name="address"]').val();

            delete  $scope.editorDocModal.timeInterval;
            delete  $scope.editorDocModal.detailAddress;

           if(!$scope.editorDocModal.startTime || !$scope.editorDocModal.endTime || !$scope.editorDocModal.reservationNumber || !$scope.editorDocModal.money || !$scope.editorDocModal.addressId){
                layer.msg('请完善信息！');
               return false;
           }



            new HttpRequest().insertOrUpdDoctorSchedualMsg($scope.editorDocModal,function (res) {
                if(res.code=='000'){
                    location.reload()
                }else{
                    layer.msg('操作失败！');
                }
            })

        }
        //删除某时段医生排班信息
        $scope.del=function () {
            new HttpRequest().deleteDoctorSchedualMsg({rowId:$scope.editorDocModal.rowId,doctorId:$scope.editorDocModal.doctorId,week:$scope.editorDocModal.week,periodTime:$scope.editorDocModal.periodTime},function (res) {
                if(res.code=='000'){
                    location.reload()
                }else{
                    layer.msg('操作失败！');
                }
            })
        }

        //电话咨询信息
        new HttpRequest().getDoctorCallMsg({wxServiceId:$scope.doctorCallRowId},function (res) {
            if(res.code=='000'){
                $scope.dataList=res.data[0] || [];
                $('.callMode').data('callData',res.data[0]);
                if(res.data){

                    $("select[name='callTime']").find("option[value='"+res.data[0].callTime+"']").attr("selected",'selected');
                }
                $scope.$apply();
            }else{
                layer.msg('操作失败');
            }
        })
        //电话咨询设置【更新/添加】
        $scope.commitCall=function () {
            var callData=$('.callMode').data('callData');
            if(callData){
                $scope.editorCallModal.rowId=callData.rowId
            }else{
                $scope.editorCallModal.rowId="";
            }
            $scope.editorCallModal.callTime=$("select[name='callTime']").val();
            $scope.editorCallModal.wxServiceId=$scope.doctorCallRowId;
            $scope.editorCallModal.money=$('input[name="money"]').val();
            //console.log($scope.editorCallModal)

            new HttpRequest().insertOrUpdDoctorCallMsg($scope.editorCallModal,function (res) {
                if(res.code=='000'){
                    location.reload()
                }else {
                    layer.msg('操作失败');
                }
            })

        }





    })
